import { NestFactory } from '@nestjs/core';
import { NestExpressApplication } from '@nestjs/platform-express';
import { AppModule } from './app.module';
import { logger } from './middleware/logger.middleware';
import * as express from 'express';
import { TransformInterceptor } from './interceptor/transform.interceptor';
import { HttpExceptionFilter } from './filter/http-exception.filter';
import { AllExceptionsFilter } from './filter/any-exception.filter';
import { DocumentBuilder, SwaggerModule } from '@nestjs/swagger';
import { join } from 'path';

async function bootstrap() {
 // const app = await NestFactory.create(AppModule);

  const app = await NestFactory.create<NestExpressApplication>(AppModule);

  //设置静态文件路径
  app.useStaticAssets(join(__dirname, '..', 'public'),{
    prefix: '/public/',   //设置虚拟路径
  });
  //设置视图文件
  app.setBaseViewsDir(join(__dirname, '..', 'views')) // 放视图的文件
  app.setViewEngine('ejs');
  //
    

  //获取参数
  app.use(express.json()); // For parsing application/json
  app.use(express.urlencoded({ extended: true })); // For parsing application/x-www-form-urlencoded
 
  // 监听所有的请求路由，并打印日志
  app.use(logger);

  // 使用全局拦截器打印出参
  app.useGlobalInterceptors(new TransformInterceptor());

  app.useGlobalFilters(new AllExceptionsFilter());
  app.useGlobalFilters(new HttpExceptionFilter());


  // 配置 Swagger
  const options = new DocumentBuilder()
  .addBearerAuth() // 开启 BearerAuth 授权认证
  .setTitle('Nest news')
  .setDescription('The nest-news API description')
  .setVersion('1.0')
  .addTag('test')
  .build();
  const document = SwaggerModule.createDocument(app, options);
  SwaggerModule.setup('api-doc', app, document);
 
  
    

  await app.listen(3003);
}
bootstrap();
